const router = require('koa-router')() // 一边导入一边实例化
// const result = require('../../config/result.js')
// const { regcheck, checking } = require('../../config/checking.js')
// 操作数据库的接口
const { getToken, Deleteurl, DeleteFileUrl, Updateurl } = require('../../config/databaseapi.js')
// 注册接口
router.post('/multipledelete', async (ctx) => {
	console.log('调用批量删除接口');
	
  let { multipleSelection, currentListId, arr } = ctx.request.body;
  // console.log(arr);
  let fileid_list = [];
  for(let i = 0; i < multipleSelection.length; i++){
	  if(multipleSelection[i].type !== 'folder'){
		  fileid_list.push(multipleSelection[i].file_id);
	  }else{
		  for(let j=0;j<multipleSelection[i].include.length;j++){
			  fileid_list.push(multipleSelection[i].include[j].file_id);
		  }
	  }
	  let query = null;
	  let res = null;
	  if(currentListId===0){
		  query = `db.collection("study_materials").doc("${multipleSelection[i]._id}").remove()`;
		  res = await new getToken().posteve(Deleteurl, query);
	  }else{
		  query = `db.collection("study_materials").doc("${currentListId}").update({data:{"include":${JSON.stringify(arr)}}})`;
		  res = await new getToken().posteve(Updateurl, query);
	  }
	  if(res.errcode !== 0){
	  			  ctx.body = { msg:'删除文件/文件夹失败', status:0 };
	  }
  }
  if(fileid_list.length > 0){
	  let ress = await new getToken().posteve3(DeleteFileUrl,fileid_list);
	  if(ress.errcode === 0){
		  ctx.body = { msg: "删除成功", status: 1 }
	  }
  }else{
	  ctx.body = { msg:"只选中了文件夹，且全部删除成功", status:1 }
  }
})

module.exports = router.routes()